<?php

namespace Modules\Common\General\SystemError;


use Modules\Common\General\Notification\SystemError\SystemErrorNofifyQueue;
use Modules\Common\General\Security\Api\Gateway;
use Modules\Common\Models\AdminUser;
use Modules\Common\Models\EmployeeUser;
use Modules\Common\Models\EnterpriseUser;
use Modules\Common\Models\SuperEnterpriseUser;
use Modules\Common\Models\SuperEnterpriseUserAccount;
use Modules\Common\Models\SystemError;
use Modules\Common\Util\Api;
use Modules\Common\Util\Database;

class ClientErrorReport
{
    /**
     * @Route(path=api/wechat/system-error/report method=post)
     * @Route(path=api/web/system-error/report method=post)
     * @Route(path=api/admin/system-error/report method=post)
     */
    public function report()
    {
        Api::validate(['title' => 'required', 'content' => 'required']);

        $clientIp = request()->getClientIp();
        $userAgent = request()->userAgent();
        $route = request()->path();
        $method = request()->method();
        if (request()->is('api/admin/*') && Gateway::user() instanceof AdminUser) {
            $user = Gateway::AdminUser();

            $userType = 3;//3：管理员
            $source = 3;//3：后台
            $userId = $user->id;
            $userName = $user->zh_name;
            $userMobile = $user->mobile;

        } elseif (request()->is('api/web/*') && Gateway::user() instanceof EnterpriseUser) {
            $user = Gateway::EnterpriseUser();
            $superEnterpriseUserAccount = SuperEnterpriseUserAccount::where('enterprise_user_id', $user->id)->first();
            $superEnterpriseUser = SuperEnterpriseUser::find($superEnterpriseUserAccount->super_enterprise_user_id);

            $userType = 2;//2：企业
            $source = 2;//2：企业端
            $userId = $user->id;
            $userName = $superEnterpriseUser->zh_name;
            $userMobile = $superEnterpriseUser->mobile;
        } elseif (request()->is('api/wechat/*') && Gateway::user() instanceof EmployeeUser) {
            $user = Gateway::EmployeeUser();
            $userType = 1;//1：个人
            $source = 1;//1：小程序
            $userId = $user->id;
            $userName = $user->employee->name;
            $userMobile = $user->mobile;
        } else {
            $userType = 5;//5：游客
            $source = 7;//7：通用接口
            $userId = Database::INT_DEFAULT;
            $userName = '游客';//5：游客
            $userMobile = request('mobile', '18000000000');
        }
        $eidDefault = auth('api')->user()->eid; 
        $sidDefault = auth('api')->user()->sid;
        $error = SystemError::createFromArray([
            'title' => request('title'),
            'content' => json_encode((array)request('content'), JSON_UNESCAPED_UNICODE),
            'type' => 2,// 2：客户端上报
            'source' => $source,
            'status' => 1,//1：未通知
            'user_id' => $userId,
            'user_type' => $userType,
            'user_ip' => $clientIp,
            'user_agent' => $userAgent,
            'user_name' => $userName,
            'user_mobile' => $userMobile,
            'route' => $route,
            'method' => $method,
            'reason_type' => 4,//4：客户端异常
            'eid'=>$eidDefault,
            'sid'=>$sidDefault,
        ]);
        if (!config('app.debug')) {
            SystemErrorNofifyQueue::dispatch($error->id)->onConnection('database')->onQueue('system-error-mail-notify');
        }
        return Api::success();
    }

}